import {getSetting, openSetting, getLocation, showModal} from "./Wx";
import CustomError from "../utils/CustomError";
module.exports = {
    /*
    * 获取用户授权信息验证
    * 检查用户是授权状况
    * 在必要的时候提醒用户重新授权（比如用户之前取消定位 在下一次场景可以通过点击再次唤起用户授权功能）
    * */
    authorization: async function (name) {
        return new Promise(async function (resolve) {
            if (await getSetting(name)) {
                resolve(true)
            } else {
                let message = "请先微信授权";
                switch (name) {
                    case "scope.userLocation":
                        message = "请先授权地理位置信息";
                        break
                    case "scope.address":
                        message = "请先授权通讯地址";
                        break
                    case "scope.record":
                        message = "请先授权录音功能";
                        break
                    case "scope.writePhotosAlbum":
                        message = "请先授权保存到相册功能";
                        break
                    case "scope.camera":
                        message = "请先授权摄像头功能";
                        break
                    default:
                        break
                }
                if (await showModal(message) && await openSetting(name)) {
                    resolve(true)
                } else {
                    resolve (false)
                }
            }
        }).then(function (res) {
            return res
        }).catch(function (err) {
            throw (new CustomError(err, "Util.getLocation")).parse()
        })
    },

    /*
    *时间戳转换成日期函数
    * 待优化
    * */
    getDate: function(t = "") { //t是要转的13位时间戳
        let date = new Date();
        if (t) {
            let str = typeof (t) !== "number" && t.indexOf ("-") !== -1 ? t.replace(/-/g, "/") : t;
            date = new Date(str);
        }
        let Y = date.getFullYear() + "-",
            M = (date.getMonth()+1 < 10 ? "0"+(date.getMonth()+1) : date.getMonth()+1) + "-",
            D = parseInt(date.getDate()) < 10 ? ("0" + date.getDate() + " ") : date.getDate() + " ",
            h = date.getHours() < 10 ? ("0" + date.getHours() + ":") : date.getHours() + ":",
            m = date.getMinutes() < 10 ? ("0" + date.getMinutes() + ":") : date.getMinutes() + ":",
            s = date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds();
        return Y + M + D + h + m + s;
    },

    /*
   * 验证手机号
   * */
    verifyPhone: function (tel) {
        return (/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(tel))
    },

    /*
    * 影藏手机号
    * */
    hiddenPhoneNumber (tel){
        let reg = /^(\d{3})\d{4}(\d{4})$/;
        return tel.replace(reg, "$1****$2");
    },

    /*
    * 验证信息
    * 字符串不为空 且 不为空格
    * 如果为空或者空白 返回fasle
    * */
    verifyInput: function (str) {
        let reg = "^[ ]+$";
        let re = new RegExp(reg);
        if (str === "" || typeof(str) === "undefined") {
            return false
        } else if (re.test(str)){
            return false
        }
        return true
    },
}